package com.ken.android.music.view.activity;


import android.animation.ObjectAnimator;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Parcelable;
import android.support.v4.media.MediaBrowserCompat;
import android.support.v4.media.session.MediaControllerCompat;
import android.support.v4.media.session.PlaybackStateCompat;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.ken.android.music.R;
import com.ken.android.music.consts.Config;
import com.ken.android.music.model.ListInfo;
import com.ken.android.music.mvp.B.BasePresenter;
import com.ken.android.music.view.fragment.ListFragment;
import com.ken.android.music.service.PlayerService;
import com.ken.android.music.service.ComponentUtil;
import com.ken.android.music.view.presenter.MusicListContract;
import com.ken.android.music.view.presenter.MusicListPresenter;
import com.ken.android.music.view.view.MusicController;


import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import androidx.annotation.Nullable;
import androidx.appcompat.widget.Toolbar;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.fragment.app.FragmentManager;


/**
 * Created by axnshy on 16/5/20.
 */
public class MusicListActivity extends BaseAudioActivity<MusicListContract.View, MusicListPresenter> implements View.OnClickListener, ListFragment.OnItemClickListener ,MusicListContract.View{


    private LinearLayout musicListLayout;
    private Context mContext;
    private Toolbar toolbar;
    private ImageView returnImg;
    private TextView listTitle;

    private MusicController controller;
    private MediaBrowserCompat mediaBrowserCompat;

//    private BottomSheetDialog bottomSheetDialog;

    WindowManager.LayoutParams params;
    Window window;

    private boolean PlayerBarToken;
    ListInfo listInfo;

    @Override
    public int onSetContent() {
        return R.layout.list_activity;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent intent = getIntent();
        listInfo = intent.getParcelableExtra("listInfo");
        mContext = this;
        params = getWindow().getAttributes();
        window = getWindow();
        initEvent();
        PlayerBarToken = true;
    }

    @Override
    public MusicListPresenter createPresenter() {
        return new MusicListPresenter();
    }

    @Override
    public void initView(View view) {
        musicListLayout = findViewById(R.id.layout_list_activity);
        returnImg = findViewById(R.id.iv_back_list_fragment);
        toolbar = findViewById(R.id.toolbar_list_fragment);
        listTitle = findViewById(R.id.tv_toolbar_title);
        controller = findViewById(R.id.controller);
        FrameLayout container = findViewById(R.id.container);
        toolbar.setTitle("");
        toolbar.setSubtitle("");
        setSupportActionBar(toolbar);
        musicListLayout.setVisibility(View.VISIBLE);
        listInfo = getIntent().getExtras().getParcelable("listinfo");
        ListFragment fragment = new ListFragment();
        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction().replace(R.id.container, fragment).commit();
        Bundle bundle = new Bundle();
        listTitle.setText(listInfo.getListName());
        bundle.putParcelable("listinfo", listInfo);
        fragment.setArguments(bundle);
    }

    private void initEvent() {
        returnImg.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        //判断Service是否存在

        if (!ComponentUtil.isMyServiceRunning(mContext, PlayerService.class)) {
            Intent serviceintent = new Intent(mContext, PlayerService.class);
            startService(serviceintent);
            Log.e("TAGS", "MediaPlayerService does not existed");
        } else {
            Log.e("TAGS", "MediaPlayerService existed");

        }
        if(v.getId() == R.id.iv_back_list_fragment) {
            onBackPressed();
        }
    }

    @Override
    public void updateToolbar(String string) {
        //topMusicDisplay.setText(string);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu2, menu);
        return true;
    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
            }
        }
    };
}